package com.yilin.tms.capital.controller;

import com.yilin.tms.capital.service.IBankCardService;
import com.yilin.commons.util.ArrayUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.model.AuthData;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.entity.capital.BankCard;
import com.yilin.tms.core.entity.capital.PayChannel;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.remote.IUserRemote;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/capital/assets/")
public class AssetsController extends BaseController {

    @Resource
    IBankCardService bankCardService;
    @Resource
    IUserRemote userRemote;

    /**
     * 获取所有资产
     * */
    @PostMapping(value = "getAssetsDetail")
    public ReturnData<Map<String,Object>> getAssetsDetail() {
        Account loginUser = getLoginUser();
        Map<String,Object> assetsMap = new HashMap<>();
        assetsMap.put("defaultCard", bankCardService.getDefaultBankCardByOrganizeId(loginUser.getOrganizeId()));
        assetsMap.put("creditScore", loginUser.getCredit());
        assetsMap.put("couponCount", 0);
        assetsMap.put("logisticsGold", 0);
        return jsonView(SUCCESS_CODE, "获取所有付款通道成功", assetsMap);
    }

    /**
     * 获取所有付款通道
     * */
    @PostMapping(value = "getPayChannelList")
    public ReturnData<List<PayChannel>> getPayChannelList() {
        List<PayChannel> payChannelList = bankCardService.getPayChannelList(getLoginUser());
        return jsonView(SUCCESS_CODE, "获取所有付款通道成功", payChannelList);
    }

    /**
     * 保存付款通道
     * */
    @PostMapping(value = "savePayChannel")
    public ReturnData<Object> savePayChannel(PayChannel payChannel) {
        if (payChannel.getBankType()==null)return jsonErrView("请选择通道银行类型");
        if (payChannel.getBankCardId() == null) return jsonErrView("请选择通道绑定的银行(卡)账户");
        BankCard bankCard = bankCardService.getBackCardById(payChannel.getBankCardId());
        if (!payChannel.getBankType().bankName.equals(bankCard.getBankName())){
            return jsonErrView("通道类型银行名称和绑定银行卡的银行名称不匹配!");
        }
        payChannel.setBankName(bankCard.getBankName());
        payChannel.setBankCardNo(bankCard.getBankAccNo());
        payChannel.setHoldCardName(bankCard.getHoldCardName());
        bankCardService.savePayChannel(getLoginUser(),payChannel);
        return jsonView(SUCCESS_CODE, "保存成功");
    }

    /**
     * 获取银行卡
     * bankShortName: 可指定银行
     * */
    @PostMapping(value = "getBankCardList")
    public ReturnData<List<BankCard>> getBankCardList(String bankShortName) {
        List<BankCard> bankCardList = bankCardService.getBackCardList(getLoginUser(),bankShortName);
        return jsonView(SUCCESS_CODE, "获取银行卡成功", bankCardList);
    }

    /**
     * 清除银行卡
     * */
    @PostMapping(value = "removeBankCardList")
    public ReturnData<List<BankCard>> removeBankCardList() {
        List<BankCard> bankCardList = bankCardService.getBackCardList(getLoginUser(),null);
        bankCardService.removeByIds(bankCardList);
        return jsonView(SUCCESS_CODE, "获取银行卡成功", bankCardList);
    }

    /**
     * 获取默认收款卡号
     * */
    @PostMapping(value = "getDefaultBankCard")
    public ReturnData<BankCard> getDefaultBankCard(String organizeId) {
        BankCard bankCard = bankCardService.getDefaultBankCardByOrganizeId(organizeId);
        return jsonView(SUCCESS_CODE, "获取银行卡成功", bankCard);
    }

    /**
     * 保存银行卡
     * */
    @PostMapping(value = "saveBankCard")
    @NotNull({"bankName","bankCode","openBankName","openAddress","cityCode","bankAccNo"})
    public ReturnData<Object> saveBankCard(BankCard bankCard) {
        userRemote.checkAuthStatus(getLoginUser().getId(), ArrayUtil.initArr(AuthData.AuthItem.authIdCard,AuthData.AuthItem.authBusinessLicense));
        bankCardService.saveBankCard(getLoginUser(),bankCard);
        return jsonView(SUCCESS_CODE, "保存成功");
    }

    /**
     * 批量获取默认收款卡号
     * */
    @PostMapping(value = "getDefaultBankCardList")
    public ReturnData<  List<BankCard>> getDefaultBankCard(String[] organizeIds) {
        List<BankCard> bankCardList = bankCardService.getDefaultBankCardListByOrganizeId(organizeIds);
        return jsonView(SUCCESS_CODE, "获取银行卡成功", bankCardList);
    }
}
